package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.dto.GoodsOrderDto;
import com.woniu.form.DownOrderForm;
import com.woniu.model.GoodsOrder;
import com.woniu.model.Result;
import com.woniu.params.DownOrderParam;
import com.woniu.service.GoodsOrderService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月25日
 */
@RestController
@Validated
@RequestMapping("/goodsOrder")
@Api(tags = "积分商品订单接口")
public class GoodsOrderController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private GoodsOrderService goodsOrderService;

    /**
     * 用户下单接口
     * @param form
     * @return
     */
    @PostMapping("/downOrder")
    @ApiOperation("用户下单积分商品")
    public Result<GoodsOrderDto> downOrder(@RequestBody @Validated DownOrderForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        DownOrderParam param = BeanUtil.toBean(form, DownOrderParam.class);
        param.setUserId(userId);
        GoodsOrderDto goodsOrders = goodsOrderService.downOrder(param);
        return Result.newInstance(GoodsOrderDto.class).setData(goodsOrders).setMessage("下单成功");
    }

    /**
     * 查看我的积分订单
     * @param token 用户token
     * @return
     * @throws Exception
     */
    @GetMapping("/myOrder")
    @ApiOperation("查看我的积分订单")
    public Result<List<GoodsOrder>> myOrder(@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        return Result.newListInstance(GoodsOrder.class).setData(goodsOrderService.list(new QueryWrapper<GoodsOrder>().eq("user_id",userId))).setMessage("查询我的订单成功");
    }

    /**
     * 积分商品的支付
     * @param orderNum 订单号
     * @param token 用户token
     * @return
     * @throws Exception
     */
    @GetMapping("/pay")
    @ApiOperation("支付积分")
    public Result<GoodsOrder> pay(@ApiParam(name = "orderNum",value = "订单编号") @RequestParam("orderNum")
                                      @NotBlank(message = "订单号不能为空") String orderNum, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("id").toString());
        GoodsOrder goodsOrder = goodsOrderService.pay(orderNum,userId);
        return Result.newInstance(GoodsOrder.class).setData(goodsOrder).setMessage("支付成功");
    }
}

